define(
	[
		'require',
		'./performance_ctrl'
	],
	function(require){
		 function actionFormatter(value, row, index) {
	        return '<div class="" style="text-align:left">'+APM.util.escape(value)+'</div>';
	    }
	    function setEmpty(value, row, index){
	    	if(value == 0){
	    		value = ''
	    	}
	    	 return '<div class="" style="text-align:right">'+APM.util.escape(value)+'</div>';
	    }
		var Controller = require('./performance_ctrl').extend({
			formTitle: "系统故障报表",
			arrayData: Ember.A(["50Count","20Count","10Count"]),
			columns: [
			  [  {
			    "field": "serviceName",
			    formatter:actionFormatter,
			    "title": "服务器",
			    "colspan": 1,
			    "rowspan": 2,
			    // "sortable": true

			  }
			  , {
			    "title": "告警数",
			    "colspan": 3,
			    "rowspan": 1
			  },
			  {
			    "title": "交易告警",
			    "colspan": 5,
			    "rowspan": 1
			  },{
			    "title": "应用系统告警",
			    "colspan": 5,
			    "rowspan": 1
			  }], 
			  [{
			    "field": "50Count",
			    "title": "严重",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,
			    // "sortable": true
			  }, {
			    "field": "20Count",
			    "title": "警告",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "10Count",
			    "title": "提醒",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,

			  }, {
			    "field": "brpmCount",
			    "title": "交易量告警",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "brttCount",
			    "title": "响应时间告警",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,

			  }, {
			    "field": "bsuccessCount",
			    "title": "成功率告警",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "berrorCount",
			    "title": "错误数告警",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,

			  }, {
			    "field": "bapdexCount",
			    "title": "Apdex告警",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "rpmCount",
			    "title": "吞吐量告警",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "rttCount",
			    "title": "响应时间告警",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "successCount",
			    "title": "成功率告警",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "errorCount",
			    "title": "错误数告警",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,
			  }, {
			    "field": "apdexCount",
			    "title": "Apdex告警",
			    formatter:setEmpty,
			    "colspan": 1,
			    "rowspan": 1,
			  }]
			],
			//根据数据来填充表格里的内容
			fillTableContent: function(node,index){
				$(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
				var tableTr = $(".table-left-container .tbody .table-left-tr").eq(index);
				var num = ((node.service.length+1)*50);
				if(index == 0){
					num = num - 1;
				}
				$(".table-left-container .tbody .table-left-tr").eq(index).css({"height":num,"line-height":num+'px'});
				if(node.appName){
					$("<div class='table-left-td'><div class='word-break'>"+node.appName+"</div></div>").appendTo(tableTr);
					$(".table-left-container .tbody .table-left-td").css("width","100%");
				}
				$(".table-left-container .tbody .table-left-tr").eq(index).find(".word-break").css("marginTop",(num-$(".word-break").eq(index).height())/2);
			},
			//根据数据插入表格头部
			fillTableHeader: function(){
				$(".table-left-container .thead").find("div").remove();
				$(".table-left-container .thead").append("<div class='clearfix table-left-tr'></div>");
				var tableTr = $(".table-left-container .thead .table-left-tr");

				$("<div class='table-left-td pull-left'>系统名称</div>").appendTo(tableTr);
				$(".table-left-container .thead .table-left-td").css("width","100%");
			},
			reportformData: function(){

				var ctrl = this;
				var params = {
					"startDate": +this.get("startDate"),
					"endDate": +this.get("endDate")
				}
				var newArray = [],
					newObject;
				clearTimeout(ctrl.get("clearTime"));
				ctrl.set("tipTitle","");

				return this.store.ajax("GET", "report/system/fault",params)
				.then(function(response){
					var performances = response.performances;

					//图表数据数据
					var chartsData = [];
					
					if(performances[0]){
						ctrl.fillTableHeader();
						$(".table-left-container .tbody").find("div").remove();
						performances.forEach(function(node,index){
							
							ctrl.fillTableContent(node,index);

							newArray.push(node);
							newObject = Ember.merge(node,{"serviceName":"合计"});
							node.service.forEach(function(app){

								newArray.push(Ember.merge(app,{"appName":node.appName}));
							});
						});
					}else{
						ctrl.set("tipTitle","这段时间内暂无数据");
						$(".table-left-container .tbody").find("div").remove();
						$(".table-left-container .tbody").append("<div class='table-left-tr clearfix'></div>");
						$(".table-left-container .tbody .table-left-tr").css("height","49px");
					
						ctrl.fillTableHeader();
						chartsData = [];
					}

					

					newArray.forEach(function(node,index){
						if(node["serviceName"] == "合计"){
							chartsData.push(node);
						}
					});
					ctrl.set("chartsData",chartsData);
					ctrl.set("groupData",newArray);
					setTimeout(function(){
						var width = $(".table-wrapper").width();
						$(".fixed-table-container tbody .no-records-found td").css({"textAlign":"left"}).css({"padding-left":(width/2-(width*0.08))-24 + "px"});
					},0);

				});
			},
			url:"report/system/fault/download",
		});

		APM.FormSystemfailureController = Controller;
		return Controller;
	}
);